package com.mspbots.api.service.impl;

import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.JsonNode;
import com.mspbots.api.service.TenantUserConfigurationService;
import com.mspbots.cw.request.TenantUserConfigurationDTO;
import com.mspbots.dto.enums.Configuration;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import reactor.core.publisher.Mono;

import java.time.Duration;
import java.util.List;
import java.util.Map;

import static java.time.temporal.ChronoUnit.SECONDS;

/**
 * com.mspbots.api.service.impl.TenantUserConfigurationServiceImpl
 *
 * @author Jun Zhou
 * @date 15:38 2019/7/1
 **/
@Deprecated
@Slf4j
@Service
public class TenantUserConfigurationServiceImpl extends BaseGenericService implements TenantUserConfigurationService {

    @Override
    public TenantUserConfigurationDTO findByKeyword(Map<String, Object> params, Configuration keyword) {
        try {
            Mono<JsonNode> resultMono = this.apiClient.get("/tenant/user-configuration/keyword/" + keyword.getKeyword(), params);
            JsonNode result = resultMono.block(Duration.of(5, SECONDS));
            return this.objectMapper.convertValue(result, TenantUserConfigurationDTO.class);
        } catch (Exception e) {
            e.printStackTrace();
            log.error("tenant user config : [{}] [{}]", params.get("tenantUserId"), keyword);
        }
        return null;

    }

    @Override
    public List<TenantUserConfigurationDTO> findListByKeyWord(Map<String, Object> tenantUserConfigParams) {
        try {
            Mono<JsonNode> resultMono = this.apiClient.get("/tenant/user-configuration/list", tenantUserConfigParams);
            JsonNode result = resultMono.block(Duration.of(5, SECONDS));
            return this.objectMapper.convertValue(result, new TypeReference<List<TenantUserConfigurationDTO>>() {
            });
        } catch (Exception e) {
            e.printStackTrace();
        }

        return null;
    }

}
